#%%
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author：albert time:2020/9/17
import pygame
import random
import sys
# import os

class GameRender:
    def __init__(self):

        pygame.init()  

        # 参数配置
        self.win_x = 1000  # 游戏窗口大小
        self.win_y = 1000
        self.FPS = 50  # FPS常量

        # 需要的图片路径
        self.grass = "../images/1.png" 
        self.sheep = "../images/2.png"  
        self.wolf = "../images/3.png" 

        # 把需要用到的图片都载入进来作为Surface对象 备用
        self.grass_surface = pygame.image.load(self.grass)
        self.sheep_surface = pygame.image.load(self.sheep)
        self.wolf_surface = pygame.image.load(self.wolf)

        # 获取每个图像的Rect对象 以便后面控制图像变动位置等等
        self.grass_rect =self.grass_surface.get_rect()
        self.sheep_rect = self.sheep_surface.get_rect()
        self.wolf_rect = self.wolf_surface.get_rect()

        # 设置各个对象的初始坐标 从Surface对象那边获取的宽高是对的 但是坐标是0,0 所以这里要设置我机的初始坐标
        print("x:{},y:{},w:{},h:{}".format(self.sheep_rect.x, self.sheep_rect.y, self.sheep_rect.w, self.sheep_rect.h))
        self.sheep_rect.x = int((self.win_x - self.sheep_rect.w)/2)  # 我机起始在最下面那行的中间位置开始
        self.sheep_rect.y = self.win_y - self.sheep_rect.h

        print("x:{},y:{},w:{},h:{}".format(self.sheep_rect.x, self.sheep_rect.y, self.sheep_rect.w, self.sheep_rect.h))

        self.firstFrame=True
        self.grassStake=[]
        self.sheepStake=[]
        self.wolfStake=[]

    def update(self,agentsStake):
        self.grassStake=agentsStake['Grass']
        self.sheepStake=agentsStake['Sheep']
        self.wolfStake=agentsStake['Wolf']
        pass
    def render(self):
        if self.firstFrame:
          # 创建游戏窗口 本质还是创建图像或者surface对象
          self.windows1 = pygame.display.set_mode((self.win_x, self.win_y), 0, 32)

          # # 一开始的游戏界面绘图
          self.windows1.fill((255,255,255))
          # self.windows1.blit(self.wolf_surface, (10, 10))  # 描绘背景
          # self.windows1.blit(self.sheep_surface, self.sheep_rect)  
          # self.windows1.blit(self.grass_surface, self.grass_rect)  
          pygame.display.update()  # 刷新窗口界面
          self.clock = pygame.time.Clock()  # 声明一个时钟对象
          self.index = 0
          self.firstFrame=False

        self.clock.tick(self.FPS)  # 每次执行到这里就会自动重置

        self.windows1.fill((255,255,255))


        for id,grass in self.grassStake.items():
          grass_surface=pygame.transform.smoothscale(self.grass_surface,(grass['energy']*5+5,grass['energy']*5+5))
          grass_rect = grass_surface.get_rect()
          self.windows1.blit(grass_surface, (grass['position'][0]*40-grass_rect.w/2,grass['position'][1]*40-grass_rect.h/2))

        for id,sheep in self.sheepStake.items():
          sheep_surface=pygame.transform.smoothscale(self.sheep_surface,(sheep['energy']*2+10,sheep['energy']*2+10))
          sheep_rect = sheep_surface.get_rect()
          self.windows1.blit(sheep_surface, (sheep['position'][0]*40-sheep_rect.w/2,sheep['position'][1]*40-sheep_rect.h/2))

        for id,wolf in self.wolfStake.items():
          wolf_surface=pygame.transform.smoothscale(self.wolf_surface,(wolf['energy']*2+5,wolf['energy']*2+5))
          self.windows1.blit(wolf_surface, (wolf['position'][0]*40,wolf['position'][1]*40))

        pygame.display.update()  # 刷新界面

        print("[{}]:耗费{}毫秒".format(self.index, self.clock.get_time()))
        self.index += 1
    def checkQuit(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
              self.quit()
    def quit(self):
      pygame.quit()
      sys.exit()
    def run(self):
        

        while True:
          # 无限循环留下一个退出的通道
          self.checkQuit()
          # print('frame')
          
          self.render()
        pygame.quit()  # 退出
# %%
if __name__=="__main__":
  render = GameRender()
  render.run()
# %%
